(function($, window, document){
	setTimeout(function(){
		$.each($(".owl-controls").find(".owl-page"), function(index, data){
			var thisControl = $(data);
			var itemIndex = $(data).parents("#owl-slider").find(".owl-item")[index];
			thisControl.attr("imgsrc", $(itemIndex).find("img").attr("src"));
		});
	}, 600);
	///Slide Show
	$.fn.swipeSlideShow = function(options){
		var defaults = {"slideItem":".slide-item", "thumbNail":true, "dragMouse":false};
		var settings = $.extend(defaults, options ||{});
		var slideWrapper = $(this);
		var slidePager = $("<ol/>", {"class":"slide-pager"});
		$.each($(this), function(){
			$(this).append(slidePager);
			$.each($(this).find(settings.slideItem), function(index, data){
				$(this).attr("index", index);
				slideWrapper.find(slidePager).append("<li class='pager-item' index='"+index+"'><a href='#'></a></li>");
			});
			$(this).find(settings.slideItem).first().addClass("active");
			$(this).find(".pager-item").first().addClass("active");
		});
		$(".pager-item").on("click", function(e){
			e.preventDefault()?e.preventDefault():e.returnValue;
			$(".pager-item").removeClass("active");
			slideWrapper.find(settings.slideItem).fadeOut(300);
			var thisIndex= $(this).attr("index");
			$(this).addClass("active");
			var slideItemW=slideWrapper.find(settings.slideItem).width();
			slideWrapper.find(settings.slideItem+"[index='"+thisIndex+"']").css({"left":slideItemW});
			slideWrapper.find(settings.slideItem+"[index='"+thisIndex+"']").show(0, function(){
				$(this).animate({"left":"0"});
			});
		});
		$.slideThumB =function(){
			var thumB = $('<div/>',{
			  "class": "slide-thumb"
			});
			$(".pager-item").on("mouseover", function(){
				var virArrow = $('<div/>',{
					"class": "slide-thumb-arrow"
				});
				var thisIndex= $(this).attr("index");
				var imgThumB = $('<img/>',{
					"src": slideWrapper.find(settings.slideItem+"[index='"+thisIndex+"']").find("img").attr("src")
				});
				$(this).prepend(thumB);
				$(thumB).html(imgThumB).append(virArrow);
			});
			$(".pager-item").on("mouseout",function(){
				$(thumB).remove(); 
			});	
		}
		if(settings.thumbNail === true){
			$.slideThumB();
		}
		if(settings.dragMouse===true){
			$(settings.slideItem).bind("mousedown", function(d){
				var elementItem = $(this);
				d.preventDefault()?d.preventDefault():d.returnValue;
				var flag =0;
				var mouseStart = d.pageX;
				$(this).on("mousemove", function(e){
					if(flag===0){
						if(mouseStart>e.pageX)
						{
							var slideItemW=$(this).width();
							$(this).css({"left":slideItemW});
							$(this).css({"left":e.pageX - $(this).offset().left});
						}
						else if(mouseStart<e.pageX){
							$(this).css({"left":0});
							$(this).css({"left":e.pageX - $(this).offset().left});
						}
					}
				});
				$(this).bind("mouseup", function(e){
					flag +=1;
					if(mouseStart>e.pageX)
					{
						var slideItemW=slideWrapper.find(settings.slideItem).width();
						if($(this).next().length>0){
							$(this).hide(0);
							$(this).prev().hide(0);
							$(this).next().css({"left":slideItemW, "right":"auto"});
							$(this).next().show(0, function(){
								$(this).animate({"left":"0"});
							});
						}
						else{
							$(this).hide(0);
							$(this).prev().hide(0);
							$(this).show(0, function(){
								$(this).animate({"left": 0, "right":"auto"});	
							});
						}
					}
					else if(mouseStart<e.pageX){
						if($(this).prev().length>0){
							$(this).hide(0);
							$(this).prev().css({"right":0, "left":"auto"});
							$(this).prev().show(0, function(){
								$(this).animate({"right":slideItemW});
							});
						}
						else{
							$(this).hide(0);
							$(this).css({"right": 0, "left":"auto"});
							$(this).show(0, function(){
								$(this).animate({"right": slideItemW, "left":"auto"});	
							});
						}
					}
				});
			});	
		}
	}
	
	///Chang Sprice
	$.changSprice=function(options){
		var defaults = {"oldVal":"50"};
		var settings = $.extend(defaults, options ||{});
		$.addSprice=function(value){
			return "Giao dịch ("+value+"K)";
		}
		$(".chk").on("change", function(){
			if($(this).is(":checked")===true)
			{
				var sprice = $.convertTextToInt($(this).parent("li").find("p").text());
				var strSprice = $.convertTextToInt($(this).parents(".split-button").find(".btn-split").val());
				var newSp = $.addSprice(strSprice + sprice);
				$(this).parents(".split-button").find(".btn-split").val(newSp);
			}
			else{
				var sprice = $.convertTextToInt($(this).parent("li").find("p").text());
				var strSprice = $.convertTextToInt($(this).parents(".split-button").find(".btn-split").val());
				var newSp = $.addSprice(strSprice - sprice);
				$(this).parents(".split-button").find(".btn-split").val(newSp);
			}
		});
	}
}(jQuery,window, document));


(function($){
	$(window).on("load",function(e) {
		$(".select-count-order, .select-each-count").cusSelect();
		$("#post-slider").swipeSlideShow();
		$.changSprice();
    });
}(jQuery));
